import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
import java.rmi.RemoteException;

public class MainForm extends MIDlet implements CommandListener
{
    public MainForm()
    {
        initialize();
    }
    
    private Form mainForm;//GEN-BEGIN:MVDFields
    private Command exitCommand;
    private Command startCommand;
    private Command stopCommand;
    private Command uploadCommand;
    private Form uploadForm;
    private Command startUploadCommand;
    private Command cancelUploadButton;
    private org.netbeans.microedition.lcdui.WaitScreen uploadWaitScreen;
    private Form resultsForm;
    private Command resultsBackCommand;
    private TextField racerNumberTextField;
    private Command saveCommand;
    private Command recordLapTimeCommand;
    private StringItem resultsList;
    private Alert uploadFailedAlert;
    private StringItem recordedLapsStringItem;
    private Spacer spacer;
    private StringItem resultsStringItem;
    private org.netbeans.microedition.util.SimpleCancellableTask uploadResultsCancellableTask;//GEN-END:MVDFields
    
    private Race _race;
//GEN-LINE:MVDMethods
    
    /** Called by the system to indicate that a command has been invoked on a particular displayable.//GEN-BEGIN:MVDCABegin
     * @param command the Command that ws invoked
     * @param displayable the Displayable on which the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:MVDCABegin
        // Insert global pre-action code here
        if (displayable == mainForm) {//GEN-BEGIN:MVDCABody
            if (command == stopCommand) {//GEN-END:MVDCABody
                // Insert pre-action code here
                // Do nothing//GEN-LINE:MVDCAAction8
                // Insert post-action code here
                _race.stop();
            } else if (command == exitCommand) {//GEN-LINE:MVDCACase8
                // Insert pre-action code here
                exitMIDlet();//GEN-LINE:MVDCAAction7
                // Insert post-action code here
            } else if (command == uploadCommand) {//GEN-LINE:MVDCACase7
                // Insert pre-action code here
                getDisplay().setCurrent(get_uploadForm());//GEN-LINE:MVDCAAction9
                // Insert post-action code here
            } else if (command == startCommand) {//GEN-LINE:MVDCACase9
                // Insert pre-action code here
                // Do nothing//GEN-LINE:MVDCAAction10
                // Insert post-action code here
                _race.start();
            } else if (command == saveCommand) {//GEN-LINE:MVDCACase10
                // Insert pre-action code here
                // Do nothing//GEN-LINE:MVDCAAction34
                // Insert post-action code here
            } else if (command == recordLapTimeCommand) {//GEN-LINE:MVDCACase34
                // Insert pre-action code here
                // Do nothing//GEN-LINE:MVDCAAction36
                // Insert post-action code here
                if(racerNumberTextField.getString() != "")
                {
                    int racerNumber = Integer.parseInt(racerNumberTextField.getString());
                    //Date time = new Date();
                    Date time = _race.recordLapTime(racerNumber);
                    recordedLapsStringItem.setText(
                            recordedLapsStringItem.getText() + "\n" +
                            "Race Number: " + racerNumber + " - " +
                            Utils.extractTimeString(time));
                    racerNumberTextField.setString("");
                }
            }//GEN-BEGIN:MVDCACase36
        } else if (displayable == resultsForm) {
            if (command == resultsBackCommand) {//GEN-END:MVDCACase36
                // Insert pre-action code here
                getDisplay().setCurrent(get_mainForm());//GEN-LINE:MVDCAAction27
                // Insert post-action code here
            }//GEN-BEGIN:MVDCACase27
        } else if (displayable == uploadForm) {
            if (command == cancelUploadButton) {//GEN-END:MVDCACase27
                // Insert pre-action code here
                getDisplay().setCurrent(get_mainForm());//GEN-LINE:MVDCAAction20
                // Insert post-action code here
            } else if (command == startUploadCommand) {//GEN-LINE:MVDCACase20
                // Insert pre-action code here
                getDisplay().setCurrent(get_uploadWaitScreen());//GEN-LINE:MVDCAAction18
                // Insert post-action code here
            }//GEN-BEGIN:MVDCACase18
        } else if (displayable == uploadWaitScreen) {
            if (command == uploadWaitScreen.FAILURE_COMMAND) {//GEN-END:MVDCACase18
                // Insert pre-action code here
                getDisplay().setCurrent(get_uploadFailedAlert(), get_uploadForm());//GEN-LINE:MVDCAAction47
                // Insert post-action code here
            } else if (command == uploadWaitScreen.SUCCESS_COMMAND) {//GEN-LINE:MVDCACase47
                // Insert pre-action code here
                getDisplay().setCurrent(get_resultsForm());//GEN-LINE:MVDCAAction46
                // Insert post-action code here
            }//GEN-BEGIN:MVDCACase46
        }//GEN-END:MVDCACase46
        // Insert global post-action code here
}//GEN-LINE:MVDCAEnd
    
    /** This method initializes UI of the application.//GEN-BEGIN:MVDInitBegin
     */
    private void initialize() {//GEN-END:MVDInitBegin
        // Insert pre-init code here
        getDisplay().setCurrent(get_mainForm());//GEN-LINE:MVDInitInit
        // Insert post-init code here
        _race = new Race();
    }//GEN-LINE:MVDInitEnd
    
    /**
     * This method should return an instance of the display.
     */
    public Display getDisplay()//GEN-FIRST:MVDGetDisplay
    {
        return Display.getDisplay(this);
    }//GEN-LAST:MVDGetDisplay
    
    /**
     * This method should exit the midlet.
     */
    public void exitMIDlet()//GEN-FIRST:MVDExitMidlet
    {
        getDisplay().setCurrent(null);
        destroyApp(true);
        notifyDestroyed();
    }//GEN-LAST:MVDExitMidlet
    
    /** This method returns instance for mainForm component and should be called instead of accessing mainForm field directly.//GEN-BEGIN:MVDGetBegin2
     * @return Instance for mainForm component
     */
    public Form get_mainForm() {
        if (mainForm == null) {//GEN-END:MVDGetBegin2
            // Insert pre-init code here
            mainForm = new Form("Race Timer", new Item[] {//GEN-BEGIN:MVDGetInit2
                get_racerNumberTextField(),
                get_spacer(),
                get_recordedLapsStringItem()
            });
            mainForm.addCommand(get_recordLapTimeCommand());
            mainForm.addCommand(get_startCommand());
            mainForm.addCommand(get_stopCommand());
            mainForm.addCommand(get_uploadCommand());
            mainForm.addCommand(get_saveCommand());
            mainForm.addCommand(get_exitCommand());
            mainForm.setCommandListener(this);//GEN-END:MVDGetInit2
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd2
        return mainForm;
    }//GEN-END:MVDGetEnd2
    
    /** This method returns instance for exitCommand component and should be called instead of accessing exitCommand field directly.//GEN-BEGIN:MVDGetBegin3
     * @return Instance for exitCommand component
     */
    public Command get_exitCommand() {
        if (exitCommand == null) {//GEN-END:MVDGetBegin3
            // Insert pre-init code here
            exitCommand = new Command("Exit", Command.EXIT, 1);//GEN-LINE:MVDGetInit3
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd3
        return exitCommand;
    }//GEN-END:MVDGetEnd3
    
    /** This method returns instance for startCommand component and should be called instead of accessing startCommand field directly.//GEN-BEGIN:MVDGetBegin4
     * @return Instance for startCommand component
     */
    public Command get_startCommand() {
        if (startCommand == null) {//GEN-END:MVDGetBegin4
            // Insert pre-init code here
            startCommand = new Command("Start", Command.ITEM, 1);//GEN-LINE:MVDGetInit4
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd4
        return startCommand;
    }//GEN-END:MVDGetEnd4
    
    /** This method returns instance for stopCommand component and should be called instead of accessing stopCommand field directly.//GEN-BEGIN:MVDGetBegin5
     * @return Instance for stopCommand component
     */
    public Command get_stopCommand() {
        if (stopCommand == null) {//GEN-END:MVDGetBegin5
            // Insert pre-init code here
            stopCommand = new Command("Stop", Command.ITEM, 1);//GEN-LINE:MVDGetInit5
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd5
        return stopCommand;
    }//GEN-END:MVDGetEnd5
    
    /** This method returns instance for uploadCommand component and should be called instead of accessing uploadCommand field directly.//GEN-BEGIN:MVDGetBegin6
     * @return Instance for uploadCommand component
     */
    public Command get_uploadCommand() {
        if (uploadCommand == null) {//GEN-END:MVDGetBegin6
            // Insert pre-init code here
            uploadCommand = new Command("Upload", Command.ITEM, 1);//GEN-LINE:MVDGetInit6
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd6
        return uploadCommand;
    }//GEN-END:MVDGetEnd6
    
    /** This method returns instance for uploadForm component and should be called instead of accessing uploadForm field directly.//GEN-BEGIN:MVDGetBegin14
     * @return Instance for uploadForm component
     */
    public Form get_uploadForm() {
        if (uploadForm == null) {//GEN-END:MVDGetBegin14
            // Insert pre-init code here
            uploadForm = new Form("Upload Results", new Item[] {get_resultsList()});//GEN-BEGIN:MVDGetInit14
            uploadForm.addCommand(get_startUploadCommand());
            uploadForm.addCommand(get_cancelUploadButton());
            uploadForm.setCommandListener(this);//GEN-END:MVDGetInit14
            // Insert post-init code here
            
            Vector v = _race.getLapTimes();
            
            for (int i=0;i<v.size();i++)
            {
                LapTime lapTime = (LapTime) v.elementAt(i);
                resultsList.setText(resultsList.getText() + "\nRacer: " + 
                        lapTime.getRacerNumber() + " Time: " + 
                        Utils.extractTimeString(lapTime.getTime()) + 
                        " Lap: " + lapTime.getLapNumber());
            }
        }//GEN-BEGIN:MVDGetEnd14
        return uploadForm;
    }//GEN-END:MVDGetEnd14
    
    /** This method returns instance for startUploadCommand component and should be called instead of accessing startUploadCommand field directly.//GEN-BEGIN:MVDGetBegin17
     * @return Instance for startUploadCommand component
     */
    public Command get_startUploadCommand() {
        if (startUploadCommand == null) {//GEN-END:MVDGetBegin17
            // Insert pre-init code here
            startUploadCommand = new Command("Start Upload", Command.ITEM, 1);//GEN-LINE:MVDGetInit17
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd17
        return startUploadCommand;
    }//GEN-END:MVDGetEnd17
    
    /** This method returns instance for cancelUploadButton component and should be called instead of accessing cancelUploadButton field directly.//GEN-BEGIN:MVDGetBegin19
     * @return Instance for cancelUploadButton component
     */
    public Command get_cancelUploadButton() {
        if (cancelUploadButton == null) {//GEN-END:MVDGetBegin19
            // Insert pre-init code here
            cancelUploadButton = new Command("Cancel", Command.CANCEL, 1);//GEN-LINE:MVDGetInit19
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd19
        return cancelUploadButton;
    }//GEN-END:MVDGetEnd19
    
    /** This method returns instance for uploadWaitScreen component and should be called instead of accessing uploadWaitScreen field directly.//GEN-BEGIN:MVDGetBegin21
     * @return Instance for uploadWaitScreen component
     */
    public org.netbeans.microedition.lcdui.WaitScreen get_uploadWaitScreen() {
        if (uploadWaitScreen == null) {//GEN-END:MVDGetBegin21
            // Insert pre-init code here
            uploadWaitScreen = new org.netbeans.microedition.lcdui.WaitScreen(getDisplay());//GEN-BEGIN:MVDGetInit21
            uploadWaitScreen.setCommandListener(this);
            uploadWaitScreen.setText("Uploading Results ...");
            uploadWaitScreen.setTask(get_uploadResultsCancellableTask());//GEN-END:MVDGetInit21
            // Insert post-init code 
        }//GEN-BEGIN:MVDGetEnd21
        return uploadWaitScreen;
    }//GEN-END:MVDGetEnd21
    
    /** This method returns instance for resultsForm component and should be called instead of accessing resultsForm field directly.//GEN-BEGIN:MVDGetBegin24
     * @return Instance for resultsForm component
     */
    public Form get_resultsForm() {
        if (resultsForm == null) {//GEN-END:MVDGetBegin24
            // Insert pre-init code here
            resultsForm = new Form("Results", new Item[] {get_resultsStringItem()});//GEN-BEGIN:MVDGetInit24
            resultsForm.addCommand(get_resultsBackCommand());
            resultsForm.setCommandListener(this);//GEN-END:MVDGetInit24
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd24
        return resultsForm;
    }//GEN-END:MVDGetEnd24
    
    
    /** This method returns instance for resultsBackCommand component and should be called instead of accessing resultsBackCommand field directly.//GEN-BEGIN:MVDGetBegin26
     * @return Instance for resultsBackCommand component
     */
    public Command get_resultsBackCommand() {
        if (resultsBackCommand == null) {//GEN-END:MVDGetBegin26
            // Insert pre-init code here
            resultsBackCommand = new Command("Back", Command.BACK, 1);//GEN-LINE:MVDGetInit26
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd26
        return resultsBackCommand;
    }//GEN-END:MVDGetEnd26
    
    /** This method returns instance for racerNumberTextField component and should be called instead of accessing racerNumberTextField field directly.//GEN-BEGIN:MVDGetBegin30
     * @return Instance for racerNumberTextField component
     */
    public TextField get_racerNumberTextField() {
        if (racerNumberTextField == null) {//GEN-END:MVDGetBegin30
            // Insert pre-init code here
            racerNumberTextField = new TextField("Racer Number:", "", 120, TextField.NUMERIC);//GEN-LINE:MVDGetInit30
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd30
        return racerNumberTextField;
    }//GEN-END:MVDGetEnd30
    
    /** This method returns instance for saveCommand component and should be called instead of accessing saveCommand field directly.//GEN-BEGIN:MVDGetBegin32
     * @return Instance for saveCommand component
     */
    public Command get_saveCommand() {
        if (saveCommand == null) {//GEN-END:MVDGetBegin32
            // Insert pre-init code here
            saveCommand = new Command("Save", Command.OK, 1);//GEN-LINE:MVDGetInit32
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd32
        return saveCommand;
    }//GEN-END:MVDGetEnd32
    
    /** This method returns instance for recordLapTimeCommand component and should be called instead of accessing recordLapTimeCommand field directly.//GEN-BEGIN:MVDGetBegin35
     * @return Instance for recordLapTimeCommand component
     */
    public Command get_recordLapTimeCommand() {
        if (recordLapTimeCommand == null) {//GEN-END:MVDGetBegin35
            // Insert pre-init code here
            recordLapTimeCommand = new Command("Record Lap Time", Command.ITEM, 1);//GEN-LINE:MVDGetInit35
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd35
        return recordLapTimeCommand;
    }//GEN-END:MVDGetEnd35
    
    /** This method returns instance for resultsList component and should be called instead of accessing resultsList field directly.//GEN-BEGIN:MVDGetBegin37
     * @return Instance for resultsList component
     */
    public StringItem get_resultsList() {
        if (resultsList == null) {//GEN-END:MVDGetBegin37
            // Insert pre-init code here
            resultsList = new StringItem("Results\n", "");//GEN-LINE:MVDGetInit37
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd37
        return resultsList;
    }//GEN-END:MVDGetEnd37
    
    /** This method returns instance for uploadFailedAlert component and should be called instead of accessing uploadFailedAlert field directly.//GEN-BEGIN:MVDGetBegin38
     * @return Instance for uploadFailedAlert component
     */
    public Alert get_uploadFailedAlert() {
        if (uploadFailedAlert == null) {//GEN-END:MVDGetBegin38
            // Insert pre-init code here
            uploadFailedAlert = new Alert(null, "Upload failed", null, AlertType.ERROR);//GEN-BEGIN:MVDGetInit38
            uploadFailedAlert.setTimeout(-2);//GEN-END:MVDGetInit38
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd38
        return uploadFailedAlert;
    }//GEN-END:MVDGetEnd38
    
    /** This method returns instance for recordedLapsStringItem component and should be called instead of accessing recordedLapsStringItem field directly.//GEN-BEGIN:MVDGetBegin40
     * @return Instance for recordedLapsStringItem component
     */
    public StringItem get_recordedLapsStringItem() {
        if (recordedLapsStringItem == null) {//GEN-END:MVDGetBegin40
            // Insert pre-init code here
            recordedLapsStringItem = new StringItem("Recorded laps", "");//GEN-LINE:MVDGetInit40
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd40
        return recordedLapsStringItem;
    }//GEN-END:MVDGetEnd40
    
    /** This method returns instance for spacer component and should be called instead of accessing spacer field directly.//GEN-BEGIN:MVDGetBegin41
     * @return Instance for spacer component
     */
    public Spacer get_spacer() {
        if (spacer == null) {//GEN-END:MVDGetBegin41
            // Insert pre-init code here
            spacer = new Spacer(1000, 1);//GEN-LINE:MVDGetInit41
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd41
        return spacer;
    }//GEN-END:MVDGetEnd41
    
    /** This method returns instance for resultsStringItem component and should be called instead of accessing resultsStringItem field directly.//GEN-BEGIN:MVDGetBegin43
     * @return Instance for resultsStringItem component
     */
    public StringItem get_resultsStringItem() {
        if (resultsStringItem == null) {//GEN-END:MVDGetBegin43
            // Insert pre-init code here
            resultsStringItem = new StringItem("", "");//GEN-LINE:MVDGetInit43
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd43
        return resultsStringItem;
    }//GEN-END:MVDGetEnd43

    /** This method returns instance for uploadResultsCancellableTask component and should be called instead of accessing uploadResultsCancellableTask field directly.//GEN-BEGIN:MVDGetBegin45
     * @return Instance for uploadResultsCancellableTask component
     */
    public org.netbeans.microedition.util.SimpleCancellableTask get_uploadResultsCancellableTask() {
        if (uploadResultsCancellableTask == null) {//GEN-END:MVDGetBegin45
            // Insert pre-init code here
            uploadResultsCancellableTask = new org.netbeans.microedition.util.SimpleCancellableTask();//GEN-BEGIN:MVDGetInit45
            uploadResultsCancellableTask.setExecutable(new org.netbeans.microedition.util.Executable() {
                public void execute() throws Exception {
                    resultsStringItem.setText(Utils.uploadResults(_race.serializeResults()));
                }
            });//GEN-END:MVDGetInit45
            // Insert post-init code here
        }//GEN-BEGIN:MVDGetEnd45
        return uploadResultsCancellableTask;
    }//GEN-END:MVDGetEnd45
    
    
    public void startApp()
    {
    }
    
    public void pauseApp()
    {
    }
    
    public void destroyApp(boolean unconditional)
    {
    }
    
}
